package 力扣._647_回文子串;

public class Solution {
    public int countSubstrings(String s) {
        //dp[i][j]: s[i][j]之间的子串是否构成回文串
        boolean[][] dp = new boolean[s.length()][s.length()];
        int count = 0;
        //初始化
        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = true;
        }
        //遍历方向从下向上，从左往右(由数据依赖关系所确定)
        for (int i = s.length() - 1; i >= 0; i--) {
            for (int j = i; j < s.length(); j++) {
                if (j - i <= 1)
                    dp[i][j] = s.charAt(i) == s.charAt(j);
                else
                    dp[i][j] = s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1];
                if (dp[i][j])
                    count++;
            }
        }
        return count;
    }
}
